package logic

import (
	"log"
	"mssgserver/constant"
	"mssgserver/db"
	"mssgserver/server/common"
	"mssgserver/server/game/model"
	"mssgserver/server/game/model/data"
)

var ArmyService = &armyService{}

type armyService struct {
}

/*
*
查询角色下边的军队
*/
func (a *armyService) GetArmys(rid int) ([]model.Army, error) {
	mr := &data.Army{}
	mrs := make([]data.Army, 0)
	err := db.Engine.Table(mr).Where("rid=?", rid).Find(&mrs)
	if err != nil {
		log.Println("GetArmys查询数据库失败")
		return make([]model.Army, 0), common.New(constant.DBError, "GetArmys查询数据库失败")
	}
	armys := make([]model.Army, 0)
	for _, army := range mrs {
		armys = append(armys, army.ToModel().(model.Army))
	}
	return armys, nil
}

/*
*
获取城市下的军队
*/
func (a *armyService) GetArmyByCity(cid int, rid int) ([]model.Army, error) {
	mr := &data.Army{}
	mrs := make([]data.Army, 0)
	err := db.Engine.Table(mr).Where("rid=? and cityId=?", rid, cid).Find(&mrs)
	if err != nil {
		log.Println("GetArmyByCity查询数据库失败")
		return make([]model.Army, 0), common.New(constant.DBError, "GetArmyByCity查询数据库失败")
	}
	armys := make([]model.Army, 0)
	for _, army := range mrs {
		armys = append(armys, army.ToModel().(model.Army))
	}
	return armys, nil
}
